package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.BaseException;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetmealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.DishVO;
import com.sky.vo.SetmealVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
@Slf4j
@Service
public class SetmealServiceImpl implements SetmealService {
    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private SetmealDishMapper setmealDishMapper;
    @Autowired
    private DishMapper dishMapper;

    @Override
    public void insert(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        setmealMapper.insert(setmeal);
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        setmealDishes.forEach(e->e.setSetmealId(setmeal.getId()));
        setmealDishMapper.insert(setmealDishes);
    }

    @Override
    public PageResult select(SetmealPageQueryDTO setmealPageQueryDTO) {
        PageHelper.startPage(setmealPageQueryDTO.getPage(),setmealPageQueryDTO.getPageSize());
        Page<SetmealVO> list = setmealMapper.pageQuery(setmealPageQueryDTO);
        return new PageResult(list.getTotal(),list.getResult());
    }

    @Override
    public void delete(List<Long> ids) {
       int count =setmealMapper.findById(ids);
       log.info("count{}",count);
       if(count > 0){
           throw new BaseException(MessageConstant.SETMEAL_ON_SALE);
       }
        setmealMapper.delete(ids);
        setmealDishMapper.delete(ids);
    }

    @Override
    public void startOrStop(Integer status, Long id) {
        if(status == StatusConstant.ENABLE){
            int count =  dishMapper.getBySetmealId(id);
            if(count > 0) throw new BaseException(MessageConstant.SETMEAL_ENABLE_FAILED);
        }
        Setmeal setmeal = Setmeal.builder().status(status).id(id).build();
        setmealMapper.update(setmeal);
    }

    @Override
    public void update(SetmealDTO setmealDTO) {
        Setmeal setmeal =new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        setmealMapper.update(setmeal);
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        if(setmealDishes == null && setmealDishes.size() == 0){
            return;
        }
        setmealDishMapper.delete(Collections.singletonList(setmeal.getId()));
        setmealDishMapper.insert(setmealDishes);
    }

    @Override
    public List<SetmealVO> list(SetmealPageQueryDTO setmealPageQueryDTO) {
        return setmealMapper.pageQuery(setmealPageQueryDTO);
    }

    @Override
    public SetmealVO getByIdWithDish(Long id) {
        SetmealVO setmealVO = setmealMapper.selectById(id);
        setmealVO.setSetmealDishes(setmealDishMapper.seekById(id));
        return setmealVO;
    }

    @Override
    public List<DishItemVO> getDishItemById(Long id) {
        Dish dish = Dish.builder().id(id).status(StatusConstant.ENABLE).build();
        List<DishVO> list = dishMapper.listWithFlavor(dish);
        List<DishItemVO> result = new ArrayList<>();
        list.forEach(e->{
            DishItemVO dishItemVO = new DishItemVO();
            BeanUtils.copyProperties(e,dishItemVO);
            result.add(dishItemVO);
        });
        return result;
    }
}
